package org.example.managementsystem.controller;

import org.example.managementsystem.model.Production_Submission_Detail;
import org.example.managementsystem.service.Production_Submission_DetailService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/production_submission_details")
@CrossOrigin(origins = "*")
public class Production_Submission_DetailController {
    private final Production_Submission_DetailService submissionDetailService;

    public Production_Submission_DetailController(Production_Submission_DetailService submissionDetailService) {
        this.submissionDetailService = submissionDetailService;
    }

    @GetMapping
    public ResponseEntity<List<Production_Submission_Detail>> getAllSubmissionDetails() {
        List<Production_Submission_Detail> details = submissionDetailService.getAllSubmissionDetails();
        return ResponseEntity.ok(details);
    }

    @GetMapping("/{detail_id}")
    public ResponseEntity<Production_Submission_Detail> getSubmissionDetailById(@PathVariable int detail_id) {
        Production_Submission_Detail detail = submissionDetailService.getSubmissionDetailById(detail_id);
        if (detail != null) {
            return ResponseEntity.ok(detail);
        }
        return ResponseEntity.notFound().build();
    }

    @PostMapping
    public ResponseEntity<Map<String, Object>> addSubmissionDetail(@RequestBody Production_Submission_Detail detail) {
        Production_Submission_Detail savedDetail = submissionDetailService.addSubmissionDetail(detail);
        Map<String, Object> response = new HashMap<>();
        response.put("detail_id", savedDetail.getDetail_id());
        response.put("message", "生产提交明细添加成功");
        return ResponseEntity.status(HttpStatus.CREATED).body(response);
    }

    @PutMapping("/{detail_id}")
    public ResponseEntity<Map<String, String>> updateSubmissionDetail(
            @PathVariable int detail_id,
            @RequestBody Production_Submission_Detail detail) {
        detail.setDetail_id(detail_id);
        boolean updated = submissionDetailService.updateSubmissionDetail(detail);

        Map<String, String> response = new HashMap<>();
        if (updated) {
            response.put("message", "生产提交明细更新成功");
            return ResponseEntity.ok(response);
        } else {
            response.put("message", "生产提交明细更新失败，未找到指定ID的明细");
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
        }
    }

    @DeleteMapping("/{detail_id}")
    public ResponseEntity<Map<String, String>> deleteSubmissionDetail(@PathVariable int detail_id) {
        boolean deleted = submissionDetailService.deleteSubmissionDetail(detail_id);

        Map<String, String> response = new HashMap<>();
        if (deleted) {
            response.put("message", "生产提交明细删除成功");
            return ResponseEntity.ok(response);
        } else {
            response.put("message", "生产提交明细删除失败，未找到指定ID的明细");
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
        }
    }

    @GetMapping("/submission/{submission_id}")
    public ResponseEntity<List<Production_Submission_Detail>> getDetailsBySubmissionId(@PathVariable int submission_id) {
        List<Production_Submission_Detail> details = submissionDetailService.getDetailsBySubmissionId(submission_id);
        return ResponseEntity.ok(details);
    }
}